package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.liaoye.hellocustomview.R;

/**
 * 放射性渐变
 */
public class RadialGradientView extends View {

    private Paint mPaint;

    public RadialGradientView(Context context) {
        super(context);
        init();
    }

    public RadialGradientView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadialGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        //设置画笔的基本属性
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);



        RadialGradient radialGradient = new RadialGradient(200, 200, 100, 0xffff0000, 0xff00ff00, Shader.TileMode.CLAMP);
        mPaint.setShader(radialGradient);
        canvas.drawCircle(200, 200, 100, mPaint);

        RadialGradient radialGradient2 = new RadialGradient(400,200,100,new int[]{0xffff0000, 0xff00ff00, 0xff0000ff}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.CLAMP);
        mPaint.setShader(radialGradient2);
        canvas.drawCircle(400, 200, 100, mPaint);


//        LinearGradient linearGradient = new LinearGradient(0, getHeight() / 2, getWidth(), getHeight() / 2, new int[]{0xffff0000, 0xff00ff00, 0xff0000ff}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.CLAMP);


        RadialGradient radialGradient3 = new RadialGradient(200,600,100,new int[]{0xffff0000, 0xff00ff00, 0xff0000ff}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.CLAMP);
        mPaint.setShader(radialGradient3);
        canvas.drawRect(new Rect(100,500,300,700), mPaint);


        RadialGradient radialGradient4 = new RadialGradient(500,600,100,new int[]{0xffff0000, 0xff00ff00, 0xff0000ff}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.MIRROR);
        mPaint.setShader(radialGradient4);
        canvas.drawRect(new Rect(400,500,600,700), mPaint);

        RadialGradient radialGradient5 = new RadialGradient(800,600,100,new int[]{0xffff0000, 0xff00ff00, 0xff0000ff}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.REPEAT);
        mPaint.setShader(radialGradient5);
        canvas.drawRect(new Rect(700,500,900,700), mPaint);

    }
}
